import sys
import pandas as pd
import numpy as np
import random
import datetime
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
import warnings
warnings.filterwarnings('ignore')

def re_type2(i='pwh'):  # 将时间格式转换为以分钟作为单位(根据有无'.'分情况讨论)
    start_idx=i.index(':')
    end_idx=i.rindex(':')
    if i.count('.')>0:
        end_idx2=i.index('.')
        hour=int(i[:start_idx])  # 提取小时数
        minute=int(i[start_idx+1:end_idx])  # 提取分钟数
        second=int(i[end_idx+1:end_idx2])  # 提取秒数
        t=hour*60+minute+second/60  # 时刻以分钟作为单位
        return t
    else:
        hour=int(i[:start_idx])  # 提取小时数
        minute=int(i[start_idx+1:end_idx])  # 提取分钟数
        second=int(i[end_idx+1:])  # 提取秒数
        t=hour*60+minute+second/60  # 时刻以分钟作为单位
        return t


file=r'C:\Users\数据处理\Desktop\dongyi\第6组.xlsx'
file2=r'C:\Users\数据处理\Desktop\性能循环\文件6.xlsx'

df=pd.read_excel(file,sheet_name='Sheet1')
print(f"df的值为{df}")

first_cycle_discharge_e=list(df[df['循环次数']==1]['放电能量Wh'])[0]    #第1圈的放电能量
first_cycle_charge_e=list(df[df['循环次数']==1]['充电能量Wh'])[0]   #第1圈的充电能量

df2_charge=df[['循环次数','充电能量保持率%']].copy()
df2_discharge=df[['循环次数','放电能量保持率%']].copy()

func_charge=interp1d(x=df2_charge['循环次数'],y=df2_charge['充电能量保持率%'],kind='cubic')
func_discharge=interp1d(x=df2_discharge['循环次数'],y=df2_discharge['放电能量保持率%'],kind='cubic')

df_new_charge=pd.DataFrame()
df_new_charge['循环次数']=[i for i in range(df2_charge['循环次数'].min(),df2_charge['循环次数'].max()+1)]
df_new_charge['充电能量保持率%']=func_charge(df_new_charge['循环次数'])
df_new_charge['充电能量(Wh)']=df_new_charge['充电能量保持率%']*first_cycle_charge_e

df_new_discharge=pd.DataFrame()
df_new_discharge['循环次数']=[i for i in range(df2_discharge['循环次数'].min(),df2_discharge['循环次数'].max()+1)]
df_new_discharge['放电能量保持率%']=func_discharge(df_new_discharge['循环次数'])
df_new_discharge['放电能量(Wh)']=df_new_discharge['放电能量保持率%']*first_cycle_discharge_e

# plt.plot(df2_charge['循环次数'],df2_charge['充电能量保持率%'],label='实际')
# plt.plot(df_new_charge['循环次数'],df_new_charge['充电能量保持率%'],label='预测')
# plt.xlabel('循环次数')
# plt.ylabel('充电能量保持率%')
# plt.legend()
# plt.show()

df_cycle=pd.read_excel(file2,sheet_name='循环层')
max_temp_T1_d=47.5
max_temp_T2_d=47.2
max_temp_T3_d=47.1

max_temp_T1_c=47.4
max_temp_T2_c=47.3
max_temp_T3_c=47.1

# print(f"df_cycle的值为{df_cycle}")
df_new=pd.DataFrame()
df_new['循环序号']=df_new_charge['循环次数']
df_new['循环序号']=df_new['循环序号'].apply(lambda x:x+2)
df_new['序号']=df_new['循环序号']
df_new['通道']=list(df_cycle['通道'].unique())[0]
df_new['开始时间']=np.nan

df_new['充电容量(Ah)']=np.nan
df_new['放电容量(Ah)']=np.nan
df_new['总容量(Ah)']=np.nan
df_new['充电比容量(Ah/kg)']=np.nan
df_new['放电比容量(Ah/kg)']=np.nan
df_new['充放电效率(%)']=np.nan

df_new['充电能量(Wh)']=df_new_charge['充电能量(Wh)']
df_new['充电能量(Wh)']=df_new['充电能量(Wh)'].round(4)
df_new['放电能量(Wh)']=df_new_discharge['放电能量(Wh)']
df_new['放电能量(Wh)']=df_new['放电能量(Wh)'].round(4)

df_new['总能量(Wh)']=abs(df_new['充电能量(Wh)']-df_new['放电能量(Wh)'])
df_new['总能量(Wh)']=df_new['总能量(Wh)'].round(4)
df_new['充电比能量(Wh/kg)']=df_new['充电能量(Wh)']
df_new['放电比能量(Wh/kg)']=df_new['放电能量(Wh)']

df_new['充电时间(h:min:s.ms)']=np.nan
df_new['放电时间(h:min:s.ms)']=np.nan

df_new['充电初始温度T1(℃)']=[random.uniform(45.3,47.5) for i in range(df_new.shape[0])]
df_new['充电初始温度T1(℃)']=df_new['充电初始温度T1(℃)'].round(1)
df_new['充电初始温度T2(℃)']=[random.uniform(45.2,47.2) for i in range(df_new.shape[0])]
df_new['充电初始温度T2(℃)']=df_new['充电初始温度T2(℃)'].round(1)
df_new['充电初始温度T3(℃)']=[random.uniform(45.1,47.1) for i in range(df_new.shape[0])]
df_new['充电初始温度T3(℃)']=df_new['充电初始温度T3(℃)'].round(1)

df_new['放电初始温度T1(℃)']=[random.uniform(45.5,47.4) for i in range(df_new.shape[0])]
df_new['放电初始温度T1(℃)']=df_new['放电初始温度T1(℃)'].round(1)
df_new['放电初始温度T2(℃)']=[random.uniform(45.2,47.3) for i in range(df_new.shape[0])]
df_new['放电初始温度T2(℃)']=df_new['放电初始温度T2(℃)'].round(1)
df_new['放电初始温度T3(℃)']=[random.uniform(45.1,47.1) for i in range(df_new.shape[0])]
df_new['放电初始温度T3(℃)']=df_new['放电初始温度T3(℃)'].round(1)

df_new['充电最大温度T1(℃)']=[random.uniform(47.3,47.5) for i in range(df_new.shape[0])]
df_new['充电最大温度T1(℃)']=df_new['充电最大温度T1(℃)'].round(1)
df_new['充电最大温度T2(℃)']=[random.uniform(47.1,47.2) for i in range(df_new.shape[0])]
df_new['充电最大温度T2(℃)']=df_new['充电最大温度T2(℃)'].round(1)
df_new['充电最大温度T3(℃)']=[random.uniform(47,47.1) for i in range(df_new.shape[0])]
df_new['充电最大温度T3(℃)']=df_new['充电最大温度T3(℃)'].round(1)

df_new['放电最大温度T1(℃)']=[random.uniform(47.3,47.4) for i in range(df_new.shape[0])]
df_new['放电最大温度T1(℃)']=df_new['放电最大温度T1(℃)'].round(1)
df_new['放电最大温度T2(℃)']=[random.uniform(47.2,47.3) for i in range(df_new.shape[0])]
df_new['放电最大温度T2(℃)']=df_new['放电最大温度T2(℃)'].round(1)
df_new['放电最大温度T3(℃)']=[random.uniform(47.1,47.2) for i in range(df_new.shape[0])]
df_new['放电最大温度T3(℃)']=df_new['放电最大温度T3(℃)'].round(1)

# print(df_new['充电最大温度T3(℃)'].unique())

df_new2=df_cycle._append(df_new,ignore_index=True)


t2_charge=pd.to_timedelta(df_new2['充电时间(h:min:s.ms)'].iloc[1])
t2_discharge=pd.to_timedelta(df_new2['放电时间(h:min:s.ms)'].iloc[1])
# df_new2['开始时间'].iloc[1]=
t2=pd.to_datetime(df_new2['开始时间'].iloc[1])
# print(t2)
# print(t2 + datetime.timedelta(hours=5, minutes=10)+t2_charge+t2_discharge)

df_new2['开始时间'].iloc[2]=t2+datetime.timedelta(hours=5, minutes=10)+t2_charge+t2_discharge   #第3个循环的开始时间
df_new2['充电时间(h:min:s.ms)'].iloc[2]=datetime.timedelta(hours=2,minutes=15,seconds=34,milliseconds=600)
df_new2['放电时间(h:min:s.ms)'].iloc[2]=datetime.timedelta(hours=2,minutes=10,seconds=24,milliseconds=800)
# 02:15:16.600	02:10:17.800
# 02:15:16.600	02:10:17.800



start_charge_e=df_new2['充电能量(Wh)'].iloc[2]     #起始充电能量
start_discharge_e=df_new2['放电能量(Wh)'].iloc[2]     #起始放电能量

basic_charge_time=df_new2['充电时间(h:min:s.ms)'].iloc[2]
basic_discharge_time=df_new2['放电时间(h:min:s.ms)'].iloc[2]

for i in range(3,df_new2.shape[0]):
    k_charge=df_new2['充电能量(Wh)'].iloc[i]/start_charge_e
    k_discharge=df_new2['放电能量(Wh)'].iloc[i]/start_discharge_e
    df_new2['充电时间(h:min:s.ms)'].iloc[i]=k_charge*basic_charge_time
    # df_new2['充电时间(h:min:s.ms)'].iloc[i] =df_new2['充电时间(h:min:s.ms)'].iloc[i].strf
    df_new2['放电时间(h:min:s.ms)'].iloc[i]=k_discharge*basic_discharge_time

    df_new2['开始时间'].iloc[i]=df_new2['开始时间'].iloc[i-1]+df_new2['充电时间(h:min:s.ms)'].iloc[i-1]+df_new2['放电时间(h:min:s.ms)'].iloc[i-1]+datetime.timedelta(minutes=20)
for i in range(3,df_new2.shape[0]):
    discharge_microsecond=df_new2['放电时间(h:min:s.ms)'].iloc[i].microseconds
    charge_microsecond=df_new2['充电时间(h:min:s.ms)'].iloc[i].microseconds
    df_new2['放电时间(h:min:s.ms)'].iloc[i]=df_new2['放电时间(h:min:s.ms)'].iloc[i]-datetime.timedelta(microseconds=(discharge_microsecond%100000)) #充电时间精确到0.1s
    df_new2['充电时间(h:min:s.ms)'].iloc[i]=df_new2['充电时间(h:min:s.ms)'].iloc[i]-datetime.timedelta(microseconds=(charge_microsecond%100000))     #放电时间精确到0.1s


# print(df_new2['开始时间'])
# print(df_new2['充电时间(h:min:s.ms)'])
# print(df_new2['放电时间(h:min:s.ms)'])
print(df_new2.info())

df_new2.to_excel(r'C:\Users\数据处理\Desktop\完成\第六组.xlsx',index=None)
print('导出成功')

# print(df_new.columns)
# print(df_cycle.columns)
# df_new=df_new[[list(df_cycle.columns)]].copy()
# print(f"df_new的值为{df_new}")


